//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2005-2006
//
// Move raster map.
//

# include "StdAfx.h"
# include "MoveImage.h"
# include "UndoRedo.h"
# include "Map.h"
# include "PtAlgo.h"

class CMoveImageAction : public CEditAction {
  public:
	CMoveImageAction (renderer_base_t * _pRenderer, const point_t & _pointShift) :
		m_pRenderer (_pRenderer),
		m_pointShift (_pointShift)
	{}

	virtual void GetName (string_t & _str) const {_str.append ("move image");}
	virtual cu_t CanUndo () const {return cuYes;}
	virtual bool Do ();
	virtual void Undo ();
	virtual void Redo () {CMoveImageAction::Do ();}

  private:
	renderer_base_t * const m_pRenderer;
	point_t const m_pointShift;
};

bool CMoveImageAction::Do () {
	CCritSec::CLock lock (g_cs);

	assert (m_pRenderer);
	m_pRenderer->rect.Shift (m_pointShift.x, m_pointShift.y);
	m_pRenderer->ptCorrectionShift.x += m_pointShift.x;
	m_pRenderer->ptCorrectionShift.y += m_pointShift.y;

	UpdateOverlays (g_map.renderers);

	return true;
}

void CMoveImageAction::Undo () {
	CCritSec::CLock lock (g_cs);

	assert (m_pRenderer);
	m_pRenderer->rect.Shift (-m_pointShift.x, -m_pointShift.y);
	m_pRenderer->ptCorrectionShift.x -= m_pointShift.x;
	m_pRenderer->ptCorrectionShift.y -= m_pointShift.y;

	UpdateOverlays (g_map.renderers);
}

////////////////////////////////////////

void MoveImage (renderer_base_t * _pRenderer, const point_t & _pointShift) {
	PerformEditAction (new CMoveImageAction (_pRenderer, _pointShift));
}